import { Table, TableBody, TableCell, TableHead, TableRow, View, } from '@aws-amplify/ui-react'; import { TableDemo } from './demo'; import { BasicExample, HighlightExample, SizeExample, SpanExample, TableStylePropExample, VariationExample, TableThemeExample, } from './examples'; import { Example, ExampleCode } from '@/components/Example'; import { Fragment } from '@/components/Fragment'; import { ComponentClassTable } from '@/components/ComponentClassTable'; import StandardHTMLAttributes from '@/components/StandardHTMLAttributes.mdx'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; ## Demo ## Usage The `Table` primitive and its various components can be used similiarly to how the HTML `table`, `tbody`, `td`, `tfoot`, `th`, `thead`, and `tr` elements are used. ```tsx file=./examples/BasicExample.tsx ```` ### Size Control the cell height and font size of a Table using the `size` prop. Available options are `small`, none (default), and `large`. ```tsx file=./examples/SizeExample.tsx ``` ### Variation The `variation` prop can be used to make all cells `bordered` or rows `striped`. Note that the `striped` variation doesn't apply to rows in a `TableHead` tag. ```tsx file=./examples/VariationExample.tsx ``` ### Highlight on Hover The `highlightOnHover` prop can be used to change the background color of table rows upon mouse hover. Note that rows in a `TableHead` tag are not highlighted. ```tsx file=./examples/HighlightExample.tsx ``` ### TableCell #### `th` and `td` cells The `TableCell` is used for all data presented in a table. If a cell is intended to be a heading for a row or column, it's recommended to set the cell as a `th` element using the `as` property: ```jsx Column Header ``` By default, the `TableCell` is rendered as a `td` element. #### Spanning multiple columns or rows The `TableCell` component can be made to span multiple columns or rows using the `colspan` or `rowspan` properties, respectively. This is similar to how the HTML native `th` and `td` elements are made to span multiple columns and rows. ```tsx file=./examples/SpanExample.tsx ``` Citrus Stone Fruit Berry Orange Nectarine Raspberry Grapefruit Apricot Blueberry
```jsx Citrus Stone Fruit Berry Orange Nectarine Raspberry Grapefruit Apricot Blueberry
```
## CSS Styling ```tsx file=./examples/TableThemeExample.tsx ``` ### Target classes ### Global Styling Each component related to the `Table` primitive has its own class name which may be used to override styling with custom CSS. Table Title
A short table description } > A B C 1 A1 B1 C1 2 A2 B2 C2 3 A3 B3 C3
```css /* styles.css */ .amplify-table__th { background-color: var(--amplify-colors-background-tertiary); } .amplify-table__th:first-child { text-align: right; } .amplify-table__row:not(:first-child) .amplify-table__th { border-top: none; } .amplify-table__row:not(:last-child) .amplify-table__th { border-bottom: none; } .amplify-table__caption { caption-side: top; text-align: right; } .table-summary { color: var(--amplify-colors-font-secondary); font-style: italic; } ``` ```jsx import { Table, TableBody, TableCell, TableRow } from '@aws-amplify/ui-react'; import '@aws-amplify/ui-react/styles.css'; import './styles.css'; Table Title
A short table description } > A B C 1 A1 B1 C1 2 A2 B2 C2 3 A3 B3 C3
; ```
### Local Styling To override styling on a specific `Table` components, you can use (in order of increasing specificity): a class selector, data attributes, or style props. _Using a class selector:_ Smaller Text Smaller Text
```css /* styles.css */ .my-custom-table .amplify-table__td { font-size: var(--amplify-font-sizes-xs); } .my-custom-table .amplify-table__row { background-color: var(--amplify-colors-neutral-60); } ``` ```jsx import './styles.css'; Smaller Text Smaller Text
; ```
_Using data attributes:_ Larger Text Larger Text
```css /* styles.css */ /* Override only large size styles */ .amplify-table[data-size='large'] .amplify-table__td { font-size: var(--amplify-font-sizes-xxxl); } ``` ```jsx import './styles.css'; Larger Text Larger Text
; ```
_Using style props:_ ```jsx file=./examples/TableStylePropExample.tsx ``` /* OR */ ```jsx file=./examples/TableFontSizePropExample.tsx ```